#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui/QApplication>
#include "ExampleScene.h"
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // set scene creation callback function. Creation function is called
    // after actual platform dependent rendering function is initializad.
    ui->widget->setSceneCreateFunc(ExampleScene::createInstance);
}

MainWindow::~MainWindow()
{
    delete ui;
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

void MainWindow::on_actionOpen_triggered()
{
    QFileDialog dlg( this, "Select file" );
    dlg.setNameFilter(tr("Stereolithography File (*.stl)"));
    dlg.setFileMode(QFileDialog::ExistingFile);
    if (dlg.exec())
    {
        QStringList fileNames = dlg.selectedFiles();

        if( fileNames.size() != 1 )
        {
            QMessageBox::about(this, tr("My App"), tr("Multiple files selected") );
            return;
        }

        QString fileName = fileNames.at(0);
        dynamic_cast<ExampleScene*>(ui->widget->getScene())->setWorkingObject(fileName);
    }
}

void MainWindow::on_actionExit_triggered()
{
    close();
}
